<template>
  <div>
    <h1>人员列表</h1>
    <h3 style="color: red">求和为：{{ sum }}</h3>
    <input type="text" placeholder="请输入名字" v-model="name">
    <button @click="addPerson(name)">添加</button>
    <button @click="addWang(name)">添加一个姓王的人</button>
    <button @click="addFromInternet">添加一人，名字随机</button>
    <ul>
      <li v-for="p in people" :key="p.id">{{ p.name }}</li>
    </ul>
  </div>
</template>

<script>
import {nanoid} from "nanoid";


export default {
  name: "PersonList",
  data() {
    return {
      name: '',
    }
  },
  props: [],
  computed: {
    /*...mapState('personList',['people']),
    ...mapState('counter', ['sum']),*/
    people() {
      return this.$store.state.personList.people;
    },
    sum() {
      return this.$store.state.counter.sum;
    },
  },
  components: {},
  methods: {
    addPerson(name) {
      if (name.trim() !== '') {
        // this.people.push({id: nanoid(), name: name});
        this.$store.commit('personList/addPerson', {id: nanoid(), name: name});
        this.name = '';
      }
    },
    addWang(name){
        this.$store.dispatch('personList/addWang', {id: nanoid(), name: name});
    },
    addFromInternet(){
        this.$store.dispatch('personList/addPersonFromServer');
    },
  },
  mixins: [],
  watch: {},
  filters: {},
  directives: {}
}
</script>

<style scoped>

</style>